package com.ruoyi.jeeplus.modules.screen.frontdesk.screen_terminal_number.service.impl;

import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_terminal_number.mapper.Frontdesk_screen_terminal_numberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘奥旸
 * @version 1.0
 */
@Service
public class Frontdesk_screen_terminal_numberService {
    @Autowired
    Frontdesk_screen_terminal_numberMapper frontdeskScreenTerminalNumberMapper;

    //计算层级
    public int calculateLevel(String parentIds) {
        // 按逗号切割字符串
        String[] ids = parentIds.split(",");
        // 返回数组的长度作为层级数量
        return ids.length;
    }

    public String getlevel(int id){
        String parentIdsById = frontdeskScreenTerminalNumberMapper.getParentIdsById(id);
        int i = calculateLevel(parentIdsById);
        if (i == 4){
            //转发到模糊查询方法
            int count = frontdeskScreenTerminalNumberMapper.countByAreaId(id);
            //总数**********统计过，如果需要判断status直接加where即可，两边都要加啊，下面还有一个方法
            System.out.println(count);
            //这地方就是返回的数据，区级别
        }else {
            //转发到其他方法
            //随机子类
            String str = Integer.toString(id);
            int child = frontdeskScreenTerminalNumberMapper.selectRandomByParentId(str);
//            System.out.println("子类===" + i1);
            String childString = Integer.toString(child);
            int count = frontdeskScreenTerminalNumberMapper.countTotalByParentId(childString);
            //总数**********如果需要判断status直接加where即可，两边都要加啊，上面还有一个方法
            System.out.println(count);
        }
        //System.out.println(i);
        return parentIdsById;
    }
    //4轮的方法
    public Map<String, Object> four_getlevel_using(int id){
        String parentIdsById = frontdeskScreenTerminalNumberMapper.getParentIdsById(id);
        int i = calculateLevel(parentIdsById);

        if (i == 4){
            DecimalFormat df = new DecimalFormat("0.00");
            //转发到模糊查询方法
            // int count = frontdeskScreenTerminalNumberMapper.countByAreaId(id);
            int rest_count =  frontdeskScreenTerminalNumberMapper.four_countByAreaId_using(id,0);
            int work_count = frontdeskScreenTerminalNumberMapper.four_countByAreaId_using(id,1);
            int out_count =  frontdeskScreenTerminalNumberMapper.four_countByAreaId_using(id,2);
//            int rest_count = frontdeskScreenTerminalNumberMapper.countByAreaId_using(id,0);
//            int work_count = frontdeskScreenTerminalNumberMapper.countByAreaId_using(id,1);
//            int out_count = frontdeskScreenTerminalNumberMapper.countByAreaId_using(id,2);
            int allCount = rest_count + work_count + out_count;

// 计算占比，注意避免除以零的情况
            double rest_ratio = (allCount != 0) ? (double) rest_count / allCount * 100 : 0;
            double work_ratio = (allCount != 0) ? (double) work_count / allCount * 100 : 0;
            double out_ratio = (allCount != 0) ? (double) out_count / allCount * 100 : 0;
// 格式化为保留两位小数
            String formatted_rest_ratio = df.format(rest_ratio);
            String formatted_work_ratio = df.format(work_ratio);
            String formatted_out_ratio = df.format(out_ratio);

// 创建一个 Map 来存储结果
            Map<String, Object> ratioMap = new HashMap<>();
            ratioMap.put("allCount", allCount);
            ratioMap.put("rest_count", rest_count);
            ratioMap.put("work_count", work_count);
            ratioMap.put("out_count", out_count);
            ratioMap.put("rest_ratio", formatted_rest_ratio);
            ratioMap.put("work_ratio", formatted_work_ratio);
            ratioMap.put("out_ratio", formatted_out_ratio);
            //总数**********统计过，如果需要判断status直接加where即可，两边都要加啊，下面还有一个方法

            return ratioMap;
            //这地方就是返回的数据，区级别
        }else {
            DecimalFormat df = new DecimalFormat("0.00");
            //转发到其他方法
            //随机子类
            String str = Integer.toString(id);
            int child = frontdeskScreenTerminalNumberMapper.selectRandomByParentId(str);

//            System.out.println("子类===" + i1);
            String childString = Integer.toString(child);
            int rest_count = frontdeskScreenTerminalNumberMapper.four_countTotalByParentId_using(childString, 0);
            int work_count = frontdeskScreenTerminalNumberMapper.four_countTotalByParentId_using(childString, 1);
            int out_count = frontdeskScreenTerminalNumberMapper.four_countTotalByParentId_using(childString, 2);
//            int rest_count = frontdeskScreenTerminalNumberMapper.countTotalByParentId_using(childString,0);
//            int work_count = frontdeskScreenTerminalNumberMapper.countTotalByParentId_using(childString,1);
//            int out_count = frontdeskScreenTerminalNumberMapper.countTotalByParentId_using(childString,2);
            int allCount = rest_count + work_count + out_count;
// 计算占比，注意避免除以零的情况
            double rest_ratio = (allCount != 0) ? (double) rest_count / allCount * 100 : 0;
            double work_ratio = (allCount != 0) ? (double) work_count / allCount * 100 : 0;
            double out_ratio = (allCount != 0) ? (double) out_count / allCount * 100 : 0;
// 格式化为保留两位小数
            String formatted_rest_ratio = df.format(rest_ratio);
            String formatted_work_ratio = df.format(work_ratio);
            String formatted_out_ratio = df.format(out_ratio);

// 创建一个 Map 来存储结果
            Map<String, Object> ratioMap = new HashMap<>();
            ratioMap.put("allCount", allCount);
            ratioMap.put("rest_count", rest_count);
            ratioMap.put("work_count", work_count);
            ratioMap.put("out_count", out_count);
            ratioMap.put("rest_ratio", formatted_rest_ratio);
            ratioMap.put("work_ratio", formatted_work_ratio);
            ratioMap.put("out_ratio", formatted_out_ratio);
            //总数**********统计过，如果需要判断status直接加where即可，两边都要加啊，下面还有一个方法

            return ratioMap;
        }

    }

//使用2轮的方法
public Map<String, Object> getlevel_using(int id){
    String parentIdsById = frontdeskScreenTerminalNumberMapper.getParentIdsById(id);
    int i = calculateLevel(parentIdsById);
    if (i == 4){
        DecimalFormat df = new DecimalFormat("0.00");
        //转发到模糊查询方法
       // int count = frontdeskScreenTerminalNumberMapper.countByAreaId(id);
        int rest_count = frontdeskScreenTerminalNumberMapper.countByAreaId_using(id,0);
        int work_count = frontdeskScreenTerminalNumberMapper.countByAreaId_using(id,1);
        int out_count = frontdeskScreenTerminalNumberMapper.countByAreaId_using(id,2);
        int allCount = rest_count + work_count + out_count;

// 计算占比，注意避免除以零的情况
        double rest_ratio = (allCount != 0) ? (double) rest_count / allCount * 100 : 0;
        double work_ratio = (allCount != 0) ? (double) work_count / allCount * 100 : 0;
        double out_ratio = (allCount != 0) ? (double) out_count / allCount * 100 : 0;
// 格式化为保留两位小数
        String formatted_rest_ratio = df.format(rest_ratio);
        String formatted_work_ratio = df.format(work_ratio);
        String formatted_out_ratio = df.format(out_ratio);

// 创建一个 Map 来存储结果
        Map<String, Object> ratioMap = new HashMap<>();
        ratioMap.put("allCount", allCount);
        ratioMap.put("rest_count", rest_count);
        ratioMap.put("work_count", work_count);
        ratioMap.put("out_count", out_count);
        ratioMap.put("rest_ratio", formatted_rest_ratio);
        ratioMap.put("work_ratio", formatted_work_ratio);
        ratioMap.put("out_ratio", formatted_out_ratio);
        //总数**********统计过，如果需要判断status直接加where即可，两边都要加啊，下面还有一个方法

        return ratioMap;
        //这地方就是返回的数据，区级别
    }else {
        DecimalFormat df = new DecimalFormat("0.00");
        //转发到其他方法
        //随机子类
        String str = Integer.toString(id);
        int child = frontdeskScreenTerminalNumberMapper.selectRandomByParentId(str);
//            System.out.println("子类===" + i1);
        String childString = Integer.toString(child);

        int rest_count = frontdeskScreenTerminalNumberMapper.countTotalByParentId_using(childString,0);
        int work_count = frontdeskScreenTerminalNumberMapper.countTotalByParentId_using(childString,1);
        int out_count = frontdeskScreenTerminalNumberMapper.countTotalByParentId_using(childString,2);
        int allCount = rest_count + work_count + out_count;
// 计算占比，注意避免除以零的情况
        double rest_ratio = (allCount != 0) ? (double) rest_count / allCount * 100 : 0;
        double work_ratio = (allCount != 0) ? (double) work_count / allCount * 100 : 0;
        double out_ratio = (allCount != 0) ? (double) out_count / allCount * 100 : 0;
// 格式化为保留两位小数
        String formatted_rest_ratio = df.format(rest_ratio);
        String formatted_work_ratio = df.format(work_ratio);
        String formatted_out_ratio = df.format(out_ratio);

// 创建一个 Map 来存储结果
        Map<String, Object> ratioMap = new HashMap<>();
        ratioMap.put("allCount", allCount);
        ratioMap.put("rest_count", rest_count);
        ratioMap.put("work_count", work_count);
        ratioMap.put("out_count", out_count);
        ratioMap.put("rest_ratio", formatted_rest_ratio);
        ratioMap.put("work_ratio", formatted_work_ratio);
        ratioMap.put("out_ratio", formatted_out_ratio);
        //总数**********统计过，如果需要判断status直接加where即可，两边都要加啊，下面还有一个方法

        return ratioMap;
    }

}



}
